﻿using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace NiggleAmp.DAP
{
    [DataContract]
    public class NAmpItem
    {
        public NAmpItem(string name, params string[] items)
            : this(name)
        {
            for (var i = 0; i < items.Length; i += 2)
            {
                if (i + 1 < items.Length)
                {
                    this[items[i]] = items[i + 1];
                }
            }
        }

        public NAmpItem(string name)
        {
            Values = new[] { new KeyValuePair<string, string>("Name", name) };
        }

        [DataMember]
        public string Name
        {
            get
            {
                return this["Name"];
            }

            set
            {
                this["Name"] = value;
            }
        }

        public KeyValuePair<string, string>[] Values { get; set; }

        public string this[string index]
        {
            get
            {
                return Values.FirstOrDefault(x => x.Key.Equals(index)).Value;
            }

            set
            {
                Values = Values
                    .Where(x => !x.Key.Equals(index))
                    .Concat(new[] { new KeyValuePair<string, string>(index, value) })
                    .ToArray();
            }
        }
    }
}
